package com.txc.books.controller;

import com.txc.books.entity.User;
import com.txc.books.exception.BaseException;
import com.txc.books.service.IUserService;
import com.txc.books.utils.PageRequest;
import com.txc.books.utils.PageResult;
import org.springframework.web.bind.annotation.*;

/**
 * @author: XCTANG
 * 会员控制类
 */

@RestController
@RequestMapping("/user")
public class UserController {

    private IUserService UserService;

    public UserController(IUserService UserService){
        this.UserService = UserService;
    }

    //查找全部会员
    @GetMapping(value = "/findAll")
    public void findAll(){
        UserService.findAll();
    }

    //添加会员信息
    @PostMapping(value = "/insert")
    public void addUser(User user){
        try{
            UserService.insert(user);
        }catch (BaseException e){
            System.out.println("出现定义的异常");
        }catch (Exception e){
            System.out.println("出现不可预料的异常");
        }
    }

    //更新会员信息
    @PostMapping(value = "/update")
    public void updateUser(User user){

        try{
            UserService.update(user);
        }catch (BaseException e){
            System.out.println("出现定义的异常");
        }catch (Exception e){
            System.out.println("出现不可预料的异常");
        }
    }


    //删除会员信息
    @GetMapping(value = "/delete")
    public void deleteUser(@RequestParam("id") String id){
        UserService.delete(Integer.parseInt(id.trim()));
    }

    //根据id查询会员信息
    @GetMapping(value = "/select")
    public void select(@RequestParam("id") String id){
        UserService.select(Integer.parseInt(id));
    }

    //分页查询
    @GetMapping(value="/findPage")
    public PageResult findPage( PageRequest pageQuery) {
        return UserService.findByPager(pageQuery);
    }

}
